﻿using System;
using System.IO;
using System.Media;
using System.Timers;
using Timer = System.Timers.Timer;
using System.Linq;
using Styx.Plugins;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Styx.Common;

namespace KordaPwnz0r
{
    public class KordaPwnz0r : HBPlugin
    {

        private LocalPlayer Me = Styx.StyxWoW.Me;
        private static Timer checkTimer, antiAFKTimer;
        private static DateTime LastSeen;
        private static bool AlreadyHaveGauntlets;
        private const string FFSuccessFightSound = "Final Fantasy VII - Victory Fanfare.wav";
        private const string RockySound = "Rocky Anthem.wav";
        private static bool RockySoundPlayed;

        public override string Name { get { return "KordaPwnz0r"; } }

        public override string Author { get { return "toNyx"; } }

        public override bool WantButton { get { return true; } }

        public override string ButtonText { get { return "Last Seen"; } }

        public override Version Version { get { return new Version(1, 0, 0); } }

        public override void Pulse() { }

        public override void Initialize()
        {
            checkTimer = new Timer(5000);
            antiAFKTimer = new Timer(50000);
            checkTimer.Elapsed += CheckForKorda;
            antiAFKTimer.Elapsed += StopAFK;
            checkTimer.Start();
            antiAFKTimer.Start();
            Lua.Events.AttachEvent("LOOT_CLOSED",CheckForGloves);
        }

        public override void Dispose()
        {
            checkTimer.Enabled = false;
            checkTimer.Elapsed -= CheckForKorda;
            checkTimer.Stop();
            checkTimer.Close();
            antiAFKTimer.Enabled = false;
            antiAFKTimer.Elapsed -= StopAFK;
            antiAFKTimer.Stop();
            antiAFKTimer.Close();
            Lua.Events.DetachEvent("LOOT_CLOSED", CheckForGloves);
        }

        public override void OnButtonPress()
        {
            Logging.Write("[{0}] Korda has been seen last time @ {1}", Name, LastSeen);
        }

        public bool HaveGlovesInBag { get { return ObjectManager.GetObjectsOfType<WoWItem>().Count(o => o.BagSlot != -1 && o.Entry == 86566) > 0; } } 
        public bool KordaTheBitchIsHereAndAlive { get { return ObjectManager.GetObjectsOfType<WoWUnit>().Count(o => o.Entry == 50332 && o.IsAlive) > 0; } }

        private void CheckForGloves(object sender, LuaEventArgs args)
        {

            if (HaveGlovesInBag && AlreadyHaveGauntlets == false)
            {
                // Checking if We already have gauntlets !
                using (var player = new SoundPlayer(Path.Combine(PluginManager.PluginsDirectory, "KordaPwnz0r\\" + FFSuccessFightSound))) { player.PlaySync(); }
                Logging.Write("[{0}] We have {1} in our bags, we don't need to kill Korda anymore ! \\o/", Name, ObjectManager.GetObjectsOfType<WoWItem>().First(o => o.Entry == 86566).Name);
                AlreadyHaveGauntlets = true;
            }
            else
            {
                RockySoundPlayed = false;
            }
        }

        private void CheckForKorda(object source, ElapsedEventArgs e)
        {
            if (AlreadyHaveGauntlets) return;

            // Security check
            if (KordaTheBitchIsHereAndAlive)
            {
                //Retrieving last time we've seen that dumbfuck shit.
                LastSeen = DateTime.Now;
                if (!RockySoundPlayed)
                    using (var player = new SoundPlayer(Path.Combine(PluginManager.PluginsDirectory, "KordaPwnz0r\\" + RockySound))) { player.Play(); }

                RockySoundPlayed = true;

                Logging.Write("[{0}] We managed to find Korda ! \\o/", Name);
            }
        }

        private void StopAFK(object source, ElapsedEventArgs e)
        {
            if (Me.IsAFKFlagged)
                Styx.Helpers.KeyboardManager.AntiAfk();
        }
    }
}
